<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>清除文件状态缓存</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.chown.html">« chown</a></li>
      <li style="float: right;"><a href="function.copy.html">copy »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>清除文件状态缓存</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.clearstatcache" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">clearstatcache</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">clearstatcache</span> &mdash; <span class="dc-title">清除文件状态缓存</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.clearstatcache-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>clearstatcache</strong></span>(<span class="methodparam"><span class="type">bool</span> <code class="parameter">$clear_realpath_cache</code><span class="initializer"> = <strong><code>false</code></strong></span></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code><span class="initializer"> = &quot;&quot;</span></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   当使用 <span class="function"><a href="function.stat.html" class="function">stat()</a></span>，<span class="function"><a href="function.lstat.html" class="function">lstat()</a></span>
   或者任何列在受影响函数表（见下面）中的函数时，PHP
   将缓存这些函数的返回信息以提供更快的性能。然而在某些情况下，你可能想清除被缓存的信息。例如如果在一个脚本中多次检查同一个文件，而该文件在此脚本执行期间有被删除或修改的危险时，你需要清除文件状态缓存。这种情况下，可以用
   <span class="function"><strong>clearstatcache()</strong></span> 函数来清除被
   PHP 缓存的该文件信息。
  </p>
  <p class="para">
   必须注意的是，对于不存在的文件，PHP 并不会缓存其信息。所以如果调用
   <span class="function"><a href="function.file-exists.html" class="function">file_exists()</a></span> 来检查不存在的文件，在该文件没有被创建之前，它都会返回
   <strong><code>false</code></strong>。如果该文件被创建了，就算以后被删除，它都会返回 <strong><code>true</code></strong>
   函数 <span class="function"><a href="function.unlink.html" class="function">unlink()</a></span> 会自动清除该缓存.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    本函数缓存特定文件名的信息，因此只在对同一个文件名进行多次操作并且需要该文件信息不被缓存时才需要调用
    <span class="function"><strong>clearstatcache()</strong></span>。
   </p>
  </p></blockquote>
  <p class="para">
   受影响的函数包括
   <span class="function"><a href="function.stat.html" class="function">stat()</a></span>，
   <span class="function"><a href="function.lstat.html" class="function">lstat()</a></span>，
   <span class="function"><a href="function.file-exists.html" class="function">file_exists()</a></span>，
   <span class="function"><a href="function.is-writable.html" class="function">is_writable()</a></span>，
   <span class="function"><a href="function.is-readable.html" class="function">is_readable()</a></span>，
   <span class="function"><a href="function.is-executable.html" class="function">is_executable()</a></span>，
   <span class="function"><a href="function.is-file.html" class="function">is_file()</a></span>，
   <span class="function"><a href="function.is-dir.html" class="function">is_dir()</a></span>，
   <span class="function"><a href="function.is-link.html" class="function">is_link()</a></span>，
   <span class="function"><a href="function.filectime.html" class="function">filectime()</a></span>，
   <span class="function"><a href="function.fileatime.html" class="function">fileatime()</a></span>，
   <span class="function"><a href="function.filemtime.html" class="function">filemtime()</a></span>，
   <span class="function"><a href="function.fileinode.html" class="function">fileinode()</a></span>，
   <span class="function"><a href="function.filegroup.html" class="function">filegroup()</a></span>，
   <span class="function"><a href="function.fileowner.html" class="function">fileowner()</a></span>，
   <span class="function"><a href="function.filesize.html" class="function">filesize()</a></span>，
   <span class="function"><a href="function.filetype.html" class="function">filetype()</a></span>
   和 <span class="function"><a href="function.fileperms.html" class="function">fileperms()</a></span>。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.clearstatcache-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">clear_realpath_cache</code></dt>

     <dd>

      <p class="para">
       是否<em class="emphasis">也</em>清除真实路径缓存
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">filename</code></dt>

     <dd>

      <p class="para">
       仅清除指定文件名的真实路径缓存; 只在
       <code class="parameter">clear_realpath_cache</code> 为 <strong><code>true</code></strong> 
       时启用。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.clearstatcache-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.clearstatcache-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2141">
    <p><strong>示例 #1 <span class="function"><strong>clearstatcache()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'output_log.txt'</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">get_owner</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stat&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stat</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">posix_getpwuid</span><span style="color: #007700">(</span><span style="color: #0000BB">$stat</span><span style="color: #007700">[</span><span style="color: #DD0000">'uid'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UID&nbsp;@&nbsp;%s:&nbsp;%s\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'r'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">get_owner</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">chown</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ross'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'r'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">get_owner</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">clearstatcache</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'r'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">get_owner</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
UID @ Sun, 12 Oct 2008 20:48:28 +0100: root
UID @ Sun, 12 Oct 2008 20:48:28 +0100: root
UID @ Sun, 12 Oct 2008 20:48:28 +0100: ross
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>